<template>
  <div class="container">
    <div class="search-box">
      <el-input
        v-model="txt"
        placeholder="请输入内容"
        class="input-box"
      ></el-input>
      <el-button>清除</el-button>
      <el-button type="primary" @click="searchBtn">搜索</el-button>
    </div>
    <el-table :data="list" border style="width: 100%">
      <el-table-column type="index" label="任务编号" width="80">
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button
            @click="editTask(row.id)"
            size="mini"
            style="margin-right: 10px"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="del(row.id)"
          >
            <template #reference>
              <el-button @click="showDelDialog" size="mini" type="danger"
                >删除</el-button
              >
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :current-page="params._page"
        :page-size="params._limit"
        @current-change="pageChange"
      >
      </el-pagination>
      <el-button @click="openDialog" size="small" plain>+ 新增</el-button>
    </div>

    <!-- 弹窗 -->
    <el-dialog
      title="弹窗"
      :visible="showDialog"
      width="30%"
      @close="closeDialog"
    >
      <el-form ref="addForm" :model="form" :rules="rules">
        <el-form-item label="任务名称" prop="name" :label-width="'120px'">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="任务描述" prop="des" :label-width="'120px'">
          <el-input v-model="form.des"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showDialog = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getUserList,
  addUser,
  getDetail,
  editUser,
  delUser,
  searchUser,
} from "@/api/user.js";
import { v4 as uuidv4 } from "uuid";
export default {
  data() {
    return {
      params: {
        _page: 1, // 当前第几页
        _limit: 5, // 每页条数
      },
      list: [], // 表格数据
      total: 0, // 总条数
      showDialog: false,
      form: {
        id: uuidv4(),
        name: "",
        des: "",
      },
      rules: {
        name: [
          { required: true, message: "任务名称不能为空", trigger: "blur" },
        ],
        des: [{ required: true, message: "任务描述不能为空", trigger: "blur" }],
      },
      visible: false,
      txt: "", // 搜索内容
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList() {
      const res = await getUserList(this.params);
      console.log(res);
      this.list = res.data;
      this.total = Number(res.headers["x-total-count"]);
    },
    pageChange(page) {
      // console.log(page);
      this.params._page = page;
      this.getList();
    },
    openDialog() {
      this.showDialog = true;
      this.form = {
        id: "",
        name: "",
        des: "",
      };
    },
    closeDialog() {
      this.showDialog = false;
      this.$refs.addForm.resetFields();
    },
    confirm() {
      // 兜底验证
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          if (this.form.id) {
            await editUser(this.form);
            this.$message.success("编辑成功");
          } else {
            await addUser(this.form);
            // console.log(res);
            this.$message.success("新增成功");
          }
          this.getList();
          this.showDialog = false;
          this.$refs.addForm.resetFields();
          this.form = {
            name: "",
            des: "",
            id: "",
          };
        }
      });
    },
    async editTask(id) {
      this.showDialog = true;
      const res = await getDetail(id);
      // console.log(res);
      this.form = res.data;
    },
    showDelDialog() {
      this.visible = true;
    },
    async del(id) {
      // console.log(1);
      await delUser(id);
      this.visible = false;
      // 更新列表
      this.getList();
      this.$message.success("删除任务成功");
    },
    async searchBtn() {
      const res = await searchUser(this.txt);
      console.log(res);
      this.list = res.data;
    },
  },
};
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
